<?php

require_once 'components/utils/xml_writer.php';

class ResponseResult
{
    public static $OK;

    public static function Init()
    {
        self::$OK = new ResponseResult();
    }
}
ResponseResult::Init();

interface IResponse
{
    /**
     * @abstract
     * @return void
     */
    function Send();

    /**
     * @abstract
     * @return string
     */
    function GetAsText();

    function SetResult(ResponseResult $result);

    function AddAsHtml($name, $value);
}

interface IResponseFactory
{
    /**
     * @abstract
     * @return IResponse
     */
    function CreateResponse();
}

class XMLResponse implements IResponse
{
    private $result;
    private $responseResult;
    private $values;

    public function __construct()
    {
        $this->result = XMLWriterFactory::CreateXMLWriter();
        $this->values = array();
    }

    /**
     * @return void
     */
    public function Send()
    {
        header('Content-Type: text/xml');
        echo $this->GetAsText();
    }

    /**
     * @return string
     */
    public function GetAsText()
    {
        $this->result->StartDocument('1.0', 'UTF-8');
        $this->result->StartElement('response');
        if ($this->responseResult == ResponseResult::$OK)
        {
            $this->result->WriteElement('type', 'OK');
        }

        foreach($this->values as $name => $value)
        {
            $this->result->WriteElement($name, $value);
        }

        $this->result->EndElement('response');
        return $this->result->GetResult();
    }

    public function AddAsHtml($name, $value)
    {
        $this->values[$name] = $value;
    }

    public function SetResult(ResponseResult $result)
    {
        $this->responseResult = $result;
    }
}

class XMLResponseFactory implements IResponseFactory
{
    /**
     * @return IResponse
     */
    function CreateResponse()
    {
        return new XMLResponse();
    }
}

?>